//
//  OPMrestListController.m
//  OnePiece
//
//  Created by wsy on 15/10/29.
//  Copyright © 2015年 wsy. All rights reserved.
//

#import "OPMrestListController.h"
#import "OPMrestListCell.h"
#import "OPRequesterManager.h"
#import "OPMrestDetailController.h"
@interface OPMrestListController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) NSMutableArray *dataSourceArr;

@end

@implementation OPMrestListController
- (NSMutableArray *)dataSourceArr{
    if (_dataSourceArr == nil) {
        _dataSourceArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArr;
}
- (void)configureData{
    NSString *str1 = @"http://www.youyuwei.com/api/label?app_code=com.youyuwei.yuwei.ios&hardware_model=%2C2&app_ver=2.1&sys_ver=9.1&type=1&device_type=ios&ywsdk_ver=20131018&open_udid=&start=0&oauth_timestamp=1446083276&list=list&device_token=&oauth_signature_method=HMAC-SHA1&x_auth_mode=client_auth&id=";
    NSString *str2 = @"&ver=2&idfa=&channel_id=App%20Store&oauth_token=&oauth_consumer_key=4&oauth_version=1.0&oauth_signature=&debug=0&oauth_nonce=";
    NSString *urlstr = [NSString stringWithFormat:@"%@%@%@",str1, self.countryid, str2];
    [OPRequesterManager requestWithType:GET urlString:urlstr parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray *array = dic[@"data"][@"list"];
        for (NSDictionary *dict in array) {
            OPMrestListModel *m = [[OPMrestListModel alloc]init];
            [m setValuesForKeysWithDictionary:dict];
            [self.dataSourceArr addObject:m];
        }
        [_mainTableView reloadData];
        self.titleLable.text = dic[@"data"][@"list"][0][@"country"];
    } error:^(NSError *error) {
        NSLog(@"%@",[error localizedDescription]);
    }];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.leftButton setImage:[UIImage imageNamed:@"meishi-back"] forState:(UIControlStateNormal)];
    [self.leftButton addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];
    // Do any additional setup after loading the view.
    _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:(UITableViewStylePlain)];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    _mainTableView.showsVerticalScrollIndicator = NO;
    _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_mainTableView];
    [_mainTableView registerClass:[OPMrestListCell class] forCellReuseIdentifier:@"cell"];
    [self configureData];
}
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectio{
    return _dataSourceArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OPMrestListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    OPMrestListModel *m = _dataSourceArr[indexPath.row];
    [cell setModel:m];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    OPMrestListModel *m = _dataSourceArr[indexPath.row];
    OPMrestDetailController *mdVC = [[OPMrestDetailController alloc]init];
    mdVC.restid = m.restid;
    [self.navigationController pushViewController:mdVC animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OPMrestListModel *model = _dataSourceArr[indexPath.row];
    return [OPMrestListCell heightForCell:model];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
